<template>
  <el-dialog :title="title" :visible.sync="dialogVisible" width="36%">
    <el-form ref="ruleForm" :model="dataForm" :rules="dataRules" label-width="100px">
      <el-form-item label="本月" prop="waterMonth">
        <el-input
          v-model="dataForm.waterMonth"
          style="width:80%"
          :disabled="disabledActive"
          placeholder="请输入本月阈值"
        ></el-input>
      </el-form-item>
      <el-form-item label="本季度" prop="waterSeason">
        <el-input
          v-model="dataForm.waterSeason"
          style="width:80%"
          :disabled="disabledActive"
          placeholder="请输入本季度阈值"
        ></el-input>
      </el-form-item>
      <el-form-item label="半年" prop="waterHalf">
        <el-input
          v-model="dataForm.waterHalf"
          style="width:80%"
          :disabled="disabledActive"
          placeholder="请输入半年阈值"
        ></el-input>
      </el-form-item>
      <el-form-item label="全年" prop="waterYear">
        <el-input
          v-model="dataForm.waterYear"
          style="width:80%"
          :disabled="disabledActive"
          placeholder="请输入全年阈值"
        ></el-input>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="dialogVisible = false">取 消</el-button>
      <el-button type="primary" @click="handleSubmit">设 置</el-button>
    </span>
  </el-dialog>
</template>

<script>
import { updateWarningById, getWarningById } from '@/api/other'
export default {
  data() {
    return {
      dialogVisible: false,
      title: '',
      type: '',
      dataForm: {
        waterMonth: '',
        waterSeason: '',
        waterHalf: '',
        waterYear: '',
      },
      dataRules: {
        waterMonth: [{ required: true, message: '请输入', trigger: 'blur' }],
        waterSeason: [{ required: true, message: '请输入', trigger: 'blur' }],
        waterHalf: [{ required: true, message: '请输入', trigger: 'blur' }],
        waterYear: [{ required: true, message: '请输入', trigger: 'blur' }],
      },
      disabledActive: false,
    }
  },
  methods: {
    init(type, item) {
      this.dialogVisible = true
      this.dialogVisible = true
      this.title = type == 'edit' ? '设置预警阀值' : '详情'
      this.type = type
      if (type == 'edit') {
        this.disabledActive = false
      } else {
        // 详情
        this.disabledActive = true
      }
      this.$nextTick((_) => {
        this.$refs['ruleForm'].resetFields()
      })
      this.getWarningByIdReq(item.id)
    },
    getWarningByIdReq(id) {
      getWarningById({ id }).then((res) => {
        const { code, data, msg } = res
        if (code !== '0000') return this.$message.error(msg)
        this.dataForm = { ...data }
      })
    },
    handleSubmit() {
      this.$refs.ruleForm.validate((valid) => {
        if (!valid) return
        let data = {
          id: this.dataForm.id,
          waterHalf: this.dataForm.waterHalf,
          waterMonth: this.dataForm.waterMonth,
          waterSeason: this.dataForm.waterSeason,
          waterYear: this.dataForm.waterYear,
          waterTypeId: this.dataForm.waterTypeResponse['id'],
        }
        updateWarningById(data).then((res) => {
          const { code, data, msg } = res
          if (code !== '0000') return this.$message.error(msg)
          this.$emit('updata')
          this.dialogVisible = false
        })
      })
    },
  },
}
</script>

<style>
</style>
